class VideoObserver < ActiveRecord::Observer
  
  def after_create(video)
    
    update_user_points(video,1)
  end
  
  def after_destroy(video)
    update_user_points(video,-1)
  end
  
  private 
  def update_user_points(video, factor)
    #get the number of points for this resource
    #Check if a campaign exists
    points = (video.user.active_campaign.points_submit_content if video.user.active_campaign) || WebappConstants::ACTIVITY_POINTS[:submitting_video]
    
    
    user = User.find(video.user_id)
    user.points = 0 unless  user.points
    user.points +=(factor*points)
    user.save!
    
    user_campaign = user.active_user_campaign
    if user_campaign
      user_campaign.points +=(factor*points)
      user_campaign.points = 0 if user_campaign.points < 0
      user_campaign.save!
    end
  end
  
end
